﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace SmallSpider.Class
{
    public class Spider
    {
        /// <summary>蜘蛛状态</summary>
        public enum SpiderState
        {
            Stop,
            Start,
            Rest
        }

        #region << 属性和成员变量 >>

        bool bThreadRunning = true;

        string _ToUrl;
        /// <summary>指定抓取的地址</summary>
        public string ToUrl
        {
            get { return _ToUrl; }
            set { _ToUrl = value; }
        }

        string _UrlSuffix;
        /// <summary>地址后缀:.com,.cn,.com.cn</summary>
        public string UrlSuffix
        {
            get { return _UrlSuffix; }
            set { _UrlSuffix = value; }
        }

        int _ThreadCount = 5;

        public int ThreadCount
        {
            get { return _ThreadCount; }
            set { _ThreadCount = value; }
        }

        SpiderState _CurrentState;
        /// <summary>当前状态</summary>
        public SpiderState CurrentState
        {
            get { return _CurrentState; }
            set { _CurrentState = value; }
        }

        public delegate void ReceiveDataHandler(string strThreadName, string strUrl, string strReceiveData, string strCharset);

        public event ReceiveDataHandler ReceivedData;

        public delegate void ThreadFinishHandler(string strThreadName);

        public event ThreadFinishHandler SpiderStop;

        static private object Locksrf2ImOb = new object();

        #endregion


        /// <summary>构造</summary>
        public Spider()
        {
        }

        /// <summary>线程方法——猎食</summary>
        public void Predation()
        {
            if (string.IsNullOrEmpty(ToUrl))
            {
                while (SpiderThread.ThreadRun)
                {
                    string strUrl = string.Empty;
                    string strGetUrl = string.Empty;
                    for (int i = 0; i <= 22 && SpiderThread.ThreadRun; i++)
                    {
                        lock (this)
                        {
                            strUrl = StaticValue.GenerateRandomNumber(i + 1);

                            if (!string.IsNullOrEmpty(UrlSuffix))
                            {
                                if (UrlSuffix.IndexOf(',') != -1)
                                {
                                    for (int h = 0; h < UrlSuffix.Split(',').Length && SpiderThread.ThreadRun; h++)
                                    {
                                        strGetUrl = "http://www." + strUrl + "." + UrlSuffix.Split(',')[h];
                                        EatPageData(strGetUrl);
                                    }
                                }
                                else
                                {
                                    strGetUrl = "http://www." + strUrl + "." + UrlSuffix;
                                    EatPageData(strGetUrl);
                                }
                            }
                            else
                            {
                                for (int h = 0; h < StaticValue.UrlPrefix.Length && SpiderThread.ThreadRun; h++)
                                {
                                    strGetUrl = "http://www." + strUrl + "." + StaticValue.UrlPrefix[h];
                                    EatPageData(strGetUrl);
                                }
                            }
                            //Thread.Sleep(3000);
                        }
                    }
                }
                if (SpiderStop != null)
                {
                    SpiderStop(Thread.CurrentThread.Name);
                }
            }
            else
            {
                EatPageData(ToUrl);
            }
        }

        void EatPageData(string strUrl)
        {
            string strCharSet = string.Empty;
            Stopwatch newstopWatch = new Stopwatch();
            newstopWatch.Start();
            string strHtmlContent = new HttpContent().GetPageData(strUrl, ref strCharSet);
            newstopWatch.Stop();
            long lonGetTime = newstopWatch.ElapsedTicks;
            if (!string.IsNullOrEmpty(strHtmlContent))
            {
                if (ReceivedData != null)
                {
                    ReceivedData(Thread.CurrentThread.Name, strUrl, strHtmlContent, strCharSet);
                }
            }
        }



    }
}
